<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft="{ selected = [] }">
      <q-btn color="primary" icon="add" unelevated @click="handleCreate">
        {{ $t('Add') }}
      </q-btn>
      <q-btn :disable="isDisable(selected)" icon="mdi-square-edit-outline" outline text-color="primary"
             @click="handleUpdate(selected[0])">
        {{ $t('Edit') }}
      </q-btn>
      <q-btn :disable="isDisable(selected)" icon="delete" outline text-color="primary"
             @click="handleDelete(selected[0])">
        {{ $t('Del') }}
      </q-btn>
      <q-btn :disable="isDisable(selected)" outline text-color="primary" @click="handleForced(selected[0])">
        {{ $t('Misc.ForceDispatch') }}
      </q-btn>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickTips') }}</q-tooltip>
      </q-icon>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="formInfo.id ? $t('Edit') : $t('Add')" :width="500" btnAlign="center"
           position="right" @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="FormRef" v-model="formInfo" :option="tableOption">
      <template v-slot:splitLine>
        <q-separator></q-separator>
      </template>
    </qx-form>
  </q-modal>
  <q-modal v-model="infoVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="rowInfo" cols="2" headerWidth="100"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n } from 'boot/i18n.js'
import { deleteAPI, getAPI, postAPI, putAPI } from 'src/api/index.js'
import { getRemoteData, setRemoteData } from 'src/enum/ajaxRemoteData.js'
import { enableOrDisable } from 'src/enum/index.js'
import { createDialog, deepClone } from 'src/utils/utils.js'
import { formatColumns } from './DispatchRuleField.js'
import { computed, onMounted, ref } from 'vue'

const rowInfo = ref({})
const formInfo = ref({})
const FormRef = ref(null)
const AGVBrands = ref([])
const searchForm = ref({})
const infoVisible = ref(false)
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/tpm/yfagv/api/aivdRule/list' })
getRemoteData('agv_brand', '/yfagv/api/agvCfgBrand/list').then(data => (AGVBrands.value = data))
const isDisable = computed(() => selected => selected.length !== 1)
const tableOption = computed(() => {
  return {
    span: 22, export: true, labelWidth: 110, searchSpan: 4, searchLabelWidth: 60, selection: 'single',
    column: [...formatColumns(AGVBrands.value), {
      label: `${i18n.t('IsEnable')}`, field: 'isEnable', type: 'radio', searchType: 'select', value: '1',
      options: enableOrDisable.value, useTag: true, topRight: true, search: false
    }, {
      label: `${i18n.t('CreationTime')}`, field: 'createdTime', type: 'daterange', display: false, minWidth: 80,
      search: false, hide: true
    }]
  }
})

const rowDblClick = row => {
  rowInfo.value = row
  infoVisible.value = true
}
const handleCreate = () => {
  modalVisible.value = true
}
const handleUpdate = row => {
  formInfo.value = deepClone(row)
  modalVisible.value = true
}
const handleDelete = row => {
  createDialog({ msg: `${i18n.t('DelTips')}` }, () => {
    deleteAPI(`/tpm/yfagv/api/aivdRule/${row.id}`, {}, () => startSearch())
  }, () => {})
}
const handleForced = row => {
  const msg = i18n.t('Misc.ForceDispatchTips')
  createDialog({ msg }, () => {
    getAPI(`/tpm/yfagv/api/aivdRule/forceDispatch/${row.id}`, {}, () => startSearch())
  }, () => {})
}
const formatSearchForm = () => {
  const params = { ...searchForm.value }
  if (searchForm.value.dateInfo?.from) {
    params.createdStartTime = params.dateInfo?.from
    params.createdEndTime = params.dateInfo?.to
    delete params.dateInfo
  }
  return { ...params }
}
const startSearch = (action, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
  action === 'clear' && setRemoteData('dispatch_area')
}
const beforeClose = () => {
  if (FormRef.value) FormRef.value.onReset()
  modalVisible.value = false
}
const submitForm = mDone => {
  FormRef.value.validate((form, done) => {
    const API = form.id ? putAPI : postAPI
    API('/tpm/yfagv/api/aivdRule', form, () => {
      startSearch('clear')
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}
onMounted(startSearch)
</script>